import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../views/Welcome";
import Users from "../views/Users";
import Rights from "../views/Rights";
import Roles from "../views/Roles";
import Cate from "../views/Cate";
import Params from "../views/Params";
import Goods from "../views/Goods";
import AddGoods from "../views/AddGoods";
import Order from "../views/Order";
import Report from "../views/Report";

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL, // 自动获取根目录路径
  routes: [
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [{
        path: '/welcome',
        component: Welcome
      },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/rights',
          component: Rights
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/categories',
          component: Cate
        },
        {
          path: '/params',
          component: Params
        },
        {
          path: '/goods',
          component: Goods
        },
        {
          path: '/goods/add',
          component: AddGoods
        },
        {
          path: '/orders',
          component: Order
        },
        {
          path: '/reports',
          component: Report
        }
      ]
    },
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next();
  } else if (!window.sessionStorage.getItem('token')) {
    return next('/login');
  } else {
    return next();
  }
})

export default router
